"""hw2.py  ejf Oct 1, 2013
 Read  skdhw1.txt, whose lines were created with:
 out = "%s:%s:%s,%s" %(page,hw,l1,l2)

 convert hw from 'hk' to slp1, using transcoder

"""
import re
import transcoder
import sys
transcoder.transcoder_set_dir("");
filename=sys.argv[1] 
fileout =sys.argv[2] 
f = open(filename,'r')
fout = open(fileout,'w')
n = 0
nout = 0 # number of headword lines written to output
asdict = {}
for line in f:
 n = n+1
 line = line.strip() # remove starting or ending whitespace
 (pagecol,hw0,line12) = re.split(':',line)
 hw = hw0
 # Standardization of AS spelling
 if False:
  hw = re.sub(r'm3','m2',hw)
  hw = re.sub(r'm5','m2',hw)
  hw = re.sub(r'ai[12]','ai',hw)
  hw = re.sub(r'e1','e',hw)
  hw = re.sub(r'o1','o',hw)
  hw = re.sub(r'r3','r',hw)
  hw = re.sub(r'p2','ph',hw)
 # some global information on the AS  encodings in hw
 lnums = re.findall("[a-z][0-9]+",hw)
 for letnum in lnums:
  if letnum not in asdict:
   asdict[letnum]=0
  v = asdict[letnum]+1
  asdict[letnum]=v

 if True:
  hwslp = transcoder.transcoder_processString(hw,'hk','slp1')
 out = "%s:%s:%s" %(pagecol,hwslp,line12)
 fout.write("%s\n" % out);
 nout = nout + 1
 # check validity of hwslp
 if re.search("[^a-zA-Z'| ]",hwslp):
  hwslp0 = hwslp
  hwslp = re.sub(r'[0-9]','',hwslp)
  print "slp problem: %s => slp='%s' changed to %s" % (line,hwslp0,hwslp)
# for debugging
f.close()
fout.close()
print "file %s has %s lines" % (filename,n)
print "%s headwords written to file %s" % (nout,fileout)
for k,v in asdict.iteritems():
 print "code %s occurs %s times" % (k,v)

